<?php

class Common_DataTable_Model_Factory {
    
    public function detectStrategy(&$mix_data) {
        
        $str_type = null;
        
        if(is_array($mix_data)) {
            
            $mix = reset($mix_data);
            if(is_array($mix) || ($mix === false)) {
                $str_type = Common_DataTable_Model::STRATEGY_ARRAY;
            }
            
        } else if(is_object($mix_data) && ($mix_data instanceof Common_DataTable_Model)) {
            
            $str_type = Common_DataTable_Model::STRATEGY_MODEL_OBJECT;
            
        } else if(is_string($mix_data)) {
            
            if(is_file($mix_data) && preg_match('/.*\.csv$/i', $mix_data)) {
                
                $str_type = Common_DataTable_Model::STRATEGY_CSV;
                
            }
            
        }
        
        return $str_type;
        
    }
    
    public function create($enm_strategy, &$mix_data, $arr_options = array()) {
        
        $obj_model = null;
        
        switch($enm_strategy) {
            
            case Common_DataTable_Model::STRATEGY_ARRAY:
                
                $obj_model = new Common_DataTable_Model_Array($mix_data);
                
                break;
                
            case Common_DataTable_Model::STRATEGY_MODEL_OBJECT:
                
                $obj_model = $mix_data;
                
                break;
                
            case Common_DataTable_Model::STRATEGY_CSV:
                
                $obj_model = new Common_DataTable_Model_Csv($mix_data, $arr_options);
                
                break;
            
        }
        
        return $obj_model;
    }
    
    static public function detectHeaderDefinition($str_key, $mix_value) {
        
        $str_label = ucwords(str_replace(array('_', '-', '.'), ' ', $str_key));
        
        $arr_header_definition = array(
            'label' => $str_label
        );
        
        if(is_numeric($mix_value)) {
            $arr_header_definition['class']  = 'number';
            $arr_header_definition['type']   = 'number';
            $arr_header_definition['format'] = 'number';
        }
        
        return $arr_header_definition;
        
    }

}
